/*Hm9gUKDDwtNjV7Mk8onAzk0GpE5soJ+BL+teTuSMJKpje/GZpyOfzSqN1XTrtObb*/
// 按钮点击事件处理
import { ajax, toast, print, output } from 'nc-lightapp-front';
import { pageConfig } from '../const';
import searchBtnClick from './searchBtnClick';
import { handleCheck_range } from '../../card/events/buttonClick';
import ampub from 'ampub';
import CreateNCUploade from 'uap/common/components/CreateNCUploade';
const { utils, components } = ampub;
const { multiLangUtils, listUtils, msgUtils } = utils;
const { getMultiLangByID } = multiLangUtils;
const { setBillFlowBtnsEnable, batchRefresh } = listUtils;
const { showMessage, showConfirm, MsgConst } = msgUtils;
const { LoginContext, ScriptReturnUtils } = components;
const { setContext } = LoginContext;
const { getScriptListReturn } = ScriptReturnUtils;
const { show: ncUploaderShow } = CreateNCUploade;
const { printFilename, tableId, pagecode, pkField, url, printNodekey, cardRouter } = pageConfig;
export default function buttonClick(props, id) {
    // 按钮事件处理
    switch (id) {
        case 'Add':
            add.call(this, props);
            break;
        case 'Delete':
            delConfirm.call(this, props);
            break;
        case 'Commit':
            CommitAction.call(this, 'SAVE', props);
            break;
        case 'UnCommit':
            CommitAction.call(this, 'UNSAVE', props);
            break;
        case 'Refresh':
            refresh.call(this, props);
            break;
        case 'Print':
            printTemp.call(this, props);
            break;
        case 'Output':
            outputTemp.call(this, props);
            break;
        case 'Attachment':
            attachment.call(this, props);
            break;
        default:
            break;
    }
}
/**
 * 新增
 * @param {*} props
 */
export function add(props) {
    linkToCard.call(this, props, undefined, 'add');
}
/**
 * @param {*} props
 */
export function delConfirm(props) {
    const checkedRows = props.table.getCheckedRows(tableId);
    if (checkedRows.length == 0) {
        showMessage.call(this, props, { type: MsgConst.Type.ChooseDelete });
        return;
    }
    showConfirm.call(this, props, {
        type: MsgConst.Type.DelSelect,
        beSureBtnClick: batchDel
    });
}
/**
 * 删除
 *
 * @param {*} props
 */
function batchDel(props) {
    const data = props.table.getCheckedRows(tableId);
    const paramInfoMap = {};
    const params = data.map((v) => {
        const id = v.data.values[pkField].value;
        const ts = v.data.values.ts.value;
        const index = v.index;
        paramInfoMap[id] = ts;
        return {
            id,
            index
        };
    });
    ajax({
        url: url.commitUrl,
        data: {
            paramInfoMap,
            dataType: 'listData',
            OperatorType: 'DELETE',
            pageid: pagecode.replace('list', 'card'),
            commitType: 'commit' // 提交
        },
        success: (res) => {
            getScriptListReturn.call(this, params, res, props, pkField, tableId.replace('list', 'card'), tableId, true);
            // 平台还没有支持批量，暂时用单个循环
            params.forEach((item) => {
                props.table.deleteCacheId(tableId, item.id);
            });
        },
        error: (res) => {
            if (res && res.message) {
                toast({ content: res.message, color: 'danger' });
            }
        }
    });
}
// 提交
export function CommitAction(OperatorType, props, content) {
    const data = props.table.getCheckedRows(tableId);
    const paramInfoMap = {};
    // 提交或收回分别需过滤出不符合其操作的数据
    const params = data.map((v) => {
        const id = v.data.values.pk_inventory.value;
        const ts = v.data.values.ts.value;
        const index = v.index;
        paramInfoMap[id] = ts;
        return {
            id,
            index
        };
    });
    let commitType = '';
    if (OperatorType === 'SAVE') {
        // 提交传这个
        commitType = 'commit';
    }
    ajax({
        url: url.commitUrl,
        data: {
            paramInfoMap,
            dataType: 'listData',
            OperatorType,
            pageid: pagecode,
            commitType,
            content
        },
        success: (res) => {
            if (content) {
                this.setState({ compositedisplay: false });
            }
            if (res && res.data && res.data.cardVos && res.data.cardVos.length > 0) {
                // 处理盘点范围多语
                res.data.cardVos.forEach((cardVo) => {
                    handleCheck_range.call(this, cardVo.head.card_head.rows[0].values.check_range, true);
                });
            }
            getScriptListReturn.call(
                this,
                params,
                res,
                props,
                pkField,
                tableId.replace('list', 'card'),
                tableId,
                false,
                pageConfig.dataSource
            );
        },
        error: (res) => {
            if (res && res.message) {
                toast({ color: 'danger', content: res.message });
            }
        }
    });
}
/**
* 刷新
* @param {*} props
*/
export function refresh(props) {
    batchRefresh.call(this, props, searchBtnClick);
}
/**
 * 打印
 * @param {*} props
 */
export function printTemp(props) {
    const printData = getPrintData.call(this, props, 'print');
    if (!printData) {
        showMessage.call(this, props, { type: MsgConst.Type.ChoosePrint });
        return;
    }
    print(
        'pdf', // 支持两类：'html'为模板打印，'pdf'为pdf打印
        url.printListUrl, // 后台打印服务url
        printData
    );
}

/**
 * 输出
 * @param {*} props
 */
export function outputTemp(props) {
    const printData = getPrintData.call(this, props, 'output');
    if (!printData) {
        showMessage.call(this, props, { type: MsgConst.Type.ChooseOutput });
        return;
    }
    output({
        url: url.printListUrl,
        data: printData
    });
}
/**
* 获取打印数据
* @param {*} props
* @param {*} outputType
*/
function getPrintData(props, outputType = 'print') {
    const checkedRows = props.table.getCheckedRows(tableId);
    if (!checkedRows || checkedRows.length == 0) {
        return false;
    }
    const pks = [];
    checkedRows.forEach((item) => {
        pks.push(item.data.values.pk_inventory.value);
    });
    const filenameValue = getMultiLangByID(printFilename);
    const printData = {
        filename: filenameValue, // 文件名称
        nodekey: printNodekey, // 模板节点标识
        oids: pks, // 需要打印数据主键
        outputType // 输出类型
    };
    return printData;
}
/**
 * 设置批量按钮是否可用
 * @param {*} props
 * @param {*} moduleId
 */
export function setBatchBtnsEnable(props, moduleId) {
    setBillFlowBtnsEnable.call(this, props, { tableId: moduleId });
}
/**
 * 跳转卡片
 * @param {*} props
 * @param {*} record
 */
export function linkToCard(props, record = {}, status = 'browse') {
    props.pushTo(cardRouter, {
        status,
        id: record[pkField] ? record[pkField].value : '',
        pagecode: pagecode.replace('list', 'card')
    });
}
/**
 * 附件管理
 * @param {*} props
 */
function attachment(props) {
    const checkedrows = props.table.getCheckedRows(tableId);
    if (!checkedrows || checkedrows.length == 0) {
        showMessage.call(this, props, { type: MsgConst.Type.ChooseOne });
        return;
    }
    // billNo 是单据编码
    const billNo = checkedrows[0].data.values.bill_code.value;
    // billId 是单据主键
    const billId = checkedrows[0].data.values[pkField].value;
    ncUploaderShow.call(this, `${pagecode}-uploader`, { billId: `fa/inventory/${billId}`, billNo });
}
/* 查看盘点结果 */
export function viewResult(props, key, record, index) {
    setContext('ts', record.ts.value);
    setContext('bill_status', record.bill_status.value);

    props.pushTo(cardRouter, {
        status: 'edit',
        current: '7',
        bill_code: record.bill_code.value,
        pk_org: record.pk_org.value,
        id: record[pkField] ? record[pkField].value : '',
        ts: record.ts.value,
        pagecode: pagecode.replace('list', 'card')
    });
}

/*Hm9gUKDDwtNjV7Mk8onAzk0GpE5soJ+BL+teTuSMJKpje/GZpyOfzSqN1XTrtObb*/